package com.das.mechanic_base.utils;

import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

public class X3TimeUtils {

    /**
     * 计算两个日期相差几天
     *
     * @param todayTime
     * @param beSpeakTime
     * @return
     */
    public static int countTime(String todayTime, String beSpeakTime) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date d1 = df.parse(todayTime);
            Date d2 = df.parse(beSpeakTime);
            long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
            long days = diff / (1000 * 60 * 60 * 24);
            return (int) days;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 转换为想要的时间
     * @param currentTime
     * @param formatType
     * @return
     * @throws ParseException
     */
    public static String longToString(long currentTime, String formatType,Locale locale)
            throws ParseException {
        Date date = longToDate(currentTime, formatType,locale); // long类型转成Date类型
        String strTime = dateToString(date, formatType,locale); // date类型转成String
        return strTime;
    }

    // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    public static Date longToDate(long currentTime, String formatType,Locale locale)
            throws ParseException {
        Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
        String sDateTime = dateToString(dateOld, formatType,locale); // 把date类型的时间转换为string
        Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
        return date;
    }

    public static String dateToString(Date data, String formatType,Locale locale) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatType,locale);
        simpleDateFormat.setTimeZone(getTimeZone());
        return simpleDateFormat.format(data);
    }

    private static TimeZone getTimeZone(){
        Calendar mDummyDate;
        mDummyDate = Calendar.getInstance();
        Calendar now = Calendar.getInstance();
        mDummyDate.setTimeZone(now.getTimeZone());
        mDummyDate.set(now.get(Calendar.YEAR), 11, 31, 13, 0, 0);
        return now.getTimeZone();
    }

    public static int countOtherTime(String todayTime, String beSpeakTime) {
        DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
        try {
            Date d1 = df.parse(todayTime);
            Date d2 = df.parse(beSpeakTime);
            long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
            long days = diff / (1000 * 60 * 60 * 24);
            return (int) days;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 转换时间
     * @param selectTime
     * @return
     */
    public static String changeTime(String selectTime){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat df1 = new SimpleDateFormat("MM月dd日 HH:mm");
        try {
            Date parse = df.parse(selectTime);
            String format = df1.format(parse);
            return format;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * 转换时间
     * @param selectTime
     * @return
     */
    public static String changeTimeS(String selectTime){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat df1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        try {
            Date parse = df.parse(selectTime);
            String format = df1.format(parse);
            return format;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * 将时间字符串转换为long
     * @param hour
     * @return
     */
    public static long fromDateStringToLong(String hour) {
        Date date = null;
        SimpleDateFormat inputFormat = new SimpleDateFormat("HH:mm");
        try {
            date = inputFormat.parse(hour);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    public static String getCurrentTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }

    /**
     * @param duration
     * @return
     */
    public static String timeParse(long duration) {
        String time = "";
        long minute = duration / 60000;
        long seconds = duration % 60000;
        long second = Math.round((float) seconds / 1000);
        if (minute < 10) {
            time += "0";
        }
        time += minute + ":";
        if (second < 10) {
            time += "0";
        }
        time += second;

        return time;
    }

    /**
     * 获取到当前时间
     *
     * @return
     */
    public static String getNowDateShort() {

        long time = System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.sql.Date d1 = new java.sql.Date(time);
        String t1 = format.format(d1);
        Log.e("msg", t1);
        return t1;

    }

    /**
     * 根据当前时间 和 直播开始的时间 进行比较 是否开始
     */
    public static Long getTimeExpend(String startTime, String endTime) {
        //传入字串类型 2016/06/28 08:30
        long longStart = getTimeMillis(startTime); //获取开始时间毫秒数
        long longEnd = getTimeMillis(endTime);  //获取结束时间毫秒数
        long longExpend = longEnd - longStart;  //获取时间差

        long longHours = longExpend / (60 * 60 * 1000); //根据时间差来计算小时数
        long longMinutes = (longExpend - longHours * (60 * 60 * 1000)) / (60 * 1000);   //根据时间差来计算分钟数
        //second = second - hour * 3600 - minute * 60

        return longExpend;
    }

    /**
     * 判断时间大小
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean getNewTimeCompareSize(String startTime, String endTime){
        boolean isBig = false;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//年-月-日 时-分
        try {
            Date date1 = dateFormat.parse(startTime);//开始时间
            Date date2 = dateFormat.parse(endTime);//结束时间
            // 1 结束时间小于开始时间 2 开始时间与结束时间相同 3 结束时间大于开始时间
            if (date2.getTime() < date1.getTime()){
                isBig = false;
            }else if (date2.getTime() >= date1.getTime()){
                //正常情况下的逻辑操作.
                isBig = true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  isBig;
    }

    public static long getTimeMillis(String strTime) {
        long returnMillis = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy'-'MM'-'dd HH':'mm':'ss");
        Date d = null;
        try {
            d = sdf.parse(strTime);
            returnMillis = d.getTime();
        } catch (ParseException e) {
        }
        return returnMillis;
    }

    public static List<String> getServiceList() {
        List<String> serviceList = new ArrayList<>();
        serviceList.add("8:00 - 8:30");
        serviceList.add("8:30 - 9:00");
        serviceList.add("9:00 - 9:30");
        serviceList.add("9:30 - 10:00");
        serviceList.add("10:00 - 10:30");
        serviceList.add("10:30 - 11:00");
        serviceList.add("11:00 - 11:30");
        serviceList.add("11:30 - 12:00");
        serviceList.add("12:00 - 12:30");
        serviceList.add("12:30 - 13:00");
        serviceList.add("13:00 - 13:30");
        serviceList.add("13:30 - 14:00");
        serviceList.add("14:00 - 14:30");
        serviceList.add("14:30 - 15:00");
        serviceList.add("15:30 - 15:30");
        serviceList.add("15:30 - 16:00");
        serviceList.add("16:00 - 16:30");
        serviceList.add("16:30 - 17:00");
        serviceList.add("17:00 - 17:30");
        serviceList.add("17:30 - 18:00");

//        serviceList.add("18:00 - 18:30");
//        serviceList.add("18:30 - 19:00");
//        serviceList.add("19:30 - 18:30");
//        serviceList.add("17:30 - 18:30");
//        serviceList.add("17:30 - 18:30");
//        serviceList.add("17:30 - 18:30");


        return serviceList;
    }

    // 2019-08-16 10:25:24
    public static long stringToLong(String strTime, String formatType)
            throws ParseException {
        if (X3StringUtils.isEmpty(strTime)) {
            return 0;
        }

        Date date = stringToDate(strTime, formatType); // String类型转成date类型
        if (date == null) {
            return 0;
        } else {
            long currentTime = dateToLong(date); // date类型转成long类型
            return currentTime;
        }
    }

    public static Date stringToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }

    // date要转换的date类型的时间
    public static long dateToLong(Date date) {
        return date.getTime();
    }

    /**
     * 判断时间大小
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean getTimeCompareSize(String startTime, String endTime){
        boolean isBig = false;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//年-月-日 时-分
        try {
            Date date1 = dateFormat.parse(startTime);//开始时间
            Date date2 = dateFormat.parse(endTime);//结束时间
            // 1 结束时间小于开始时间 2 开始时间与结束时间相同 3 结束时间大于开始时间
            if (date2.getTime() < date1.getTime()){
                isBig = false;
            }else if (date2.getTime() >= date1.getTime()){
                //正常情况下的逻辑操作.
                isBig = true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  isBig;
    }

    /**
     * 获取两个日期相差的月数
     */
    public  static int getMonthDiff(Date d1, Date d2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        // 获取年的差值
        int yearInterval = year1 - year2;
        // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
        if (month1 < month2 || month1 == month2 && day1 < day2) {
            yearInterval--;
        }
        // 获取月数差值
        int monthInterval = (month1 + 12) - month2;
        if (day1 < day2) {
            monthInterval--;
        }
        monthInterval %= 12;
        int monthsDiff = Math.abs(yearInterval * 12 + monthInterval);
        return monthsDiff;
    }
}
